let sysStatus = [
  {
    label: '停用',
    value: '0',
  },
  {
    label: '启用',
    value: '1',
  },
]

let filter = {
  title: '',
  body: [
    {
      type: 'group',
      body: [
        {
          type: 'input-text',
          name: 'sys_code',
          placeholder: '系统编号',
          size: 'sm',
          clearable: true,
        },
        {
          placeholder: '系统名称',
          type: 'input-text',
          name: 'sys_name',
          clearable: true,
          size: 'md',
        },
        {
          name: 'sys_leader',
          placeholder: '责任人',
          type: 'input-text',
          clearable: true,
        },
        {
          type: 'input-text',
          name: 'sys_desc',
          clearable: true,
          placeholder: '描述',
          size: 'md',
        },
        {
          placeholder: '状态',
          type: 'select',
          name: 'sys_status',
          clearable: true,
          options: sysStatus,
        },

        {
          type: 'button-toolbar',
          buttons: [
            {
              type: 'reset',
              label: '重置',
            },
            {
              type: 'submit',
              level: 'primary',
              label: '查询',
            },
          ],
        },
      ],
    },
  ],
  actions: [],
}

let addDialog = {
  label: '新增',
  type: 'button',
  actionType: 'dialog',
  level: 'success',
  dialog: {
    title: '新增系统',
    size: 'md',
    body: {
      type: 'form',
      name: 'form',
      api: {
        method: 'post',
        url: '/_dev/system/add',
        data: {
          id: '${id|default:undefined}',
          sys_code: '${sys_code|default:undefined}',
          sys_name: '${sys_name|default:undefined}',
          sys_leader: '${sys_leader|default:undefined}',
          sys_desc: '${sys_desc|default:undefined}',
        },
      },
      body: [
        {
          type: 'uuid',
          name: 'id',
        },
        {
          label: '系统编号',
          type: 'input-text',
          name: 'sys_code',
          maxLength: 16,
          showCounter: true,
          clearValueOnEmpty: true,
          required: true,
        },
        {
          label: '系统名称',
          type: 'input-text',
          name: 'sys_name',
          required: true,
          showCounter: true,
          maxLength: 32,
          clearValueOnEmpty: true,
        },
        {
          label: '责任人',
          type: 'input-text',
          name: 'sys_leader',
          required: true,
          showCounter: true,
          clearValueOnEmpty: true,
          maxLength: 16,
        },
        {
          type: 'input-text',
          name: 'sys_desc',
          label: '系统描述',
          showCounter: true,
          maxLength: 128,
          clearValueOnEmpty: true,
          required: true,
        },
      ],
    },
  },
}

let editDialog = {
  label: '编辑',
  type: 'button',
  actionType: 'dialog',
  level: 'info',
  dialog: {
    title: '编辑系统',
    size: 'md',
    body: {
      type: 'form',
      name: 'form',
      api: {
        method: 'post',
        url: '/_dev/system/edit',
        data: {
          id: '${id|default:undefined}',
          sys_code: '${sys_code|default:undefined}',
          sys_name: '${sys_name|default:undefined}',
          sys_leader: '${sys_leader|default:undefined}',
          sys_desc: '${sys_desc|default:undefined}',
        },
      },
      body: [
        {
          label: '系统编号',
          type: 'input-text',
          name: 'sys_code',
          maxLength: 16,
          required: true,
        },
        {
          label: '系统名称',
          type: 'input-text',
          name: 'sys_name',
          required: true,
          maxLength: 32,
        },

        {
          label: '责任人',
          type: 'input-text',
          name: 'sys_leader',
          required: true,
          clearValueOnEmpty: true,
          maxLength: 16,
        },
        {
          type: 'input-text',
          name: 'sys_desc',
          label: '系统描述',
          showCounter: true,
          maxLength: 128,
          clearValueOnEmpty: true,
          required: true,
          columnRatio: 6,
        },
      ],
    },
  },
}

let delDialog = {
  label: '删除',
  type: 'button',
  actionType: 'ajax',
  level: 'danger',
  visibleOn: '${sys_status===0}',
  confirmText: '确认删除系统[${sys_name}]?',
  api: {
    method: 'post',
    url: '/_dev/system/del',
    data: {
      id: '${id}',
    },
  },
}

let disableButton = {
  label: '停用',
  type: 'button',
  actionType: 'ajax',
  level: 'warning',
  visibleOn: '${sys_status===1}',
  confirmText: '确认停用系统[${sys_name}]?',
  api: {
    method: 'post',
    url: '/_dev/system/edit',
    data: {
      id: '${id}',
      sys_status: '0',
    },
  },
}

let enableButton = {
  label: '启用',
  type: 'button',
  actionType: 'ajax',
  level: 'success',
  visibleOn: '${sys_status===0}',
  confirmText: '确认启用系统[${sys_name}]？',
  api: {
    method: 'post',
    url: '/_dev/system/edit',
    data: {
      id: '${id}',
      sys_status: '1',
    },
  },
}

let exportLink = {
  label: '导出',
  type: "action",
  level: "success",
  actionType: "download",
  api: '/_dev/system/export?id=${id}'
}

let importDialog = {
  label: '导入',
  type: 'button',
  actionType: 'dialog',
  level: 'warning',
  dialog: {
    title: '导入系统',
    size: 'md',
    body: {
      type: 'form',
      name: 'form',
      api: {
        method: 'post',
        url: '/_dev/system/import',
        data: {
          input_file: '${input_file}'
        },
      },
      body: [
        {
          label: '系统文件',
          type: 'input-file',
          name: 'input_file',
          accept: ".json",
          required: true,
          asBase64: true,
          maxSize: 1048576,
        },
      ],
    },
  },
}


let systemTable = {
  type: 'crud',
  mode: 'table',
  perPage: 10,
  autoFillHeight: true,
  affixHeader: true,
  alwaysShowPagination: true,
  api: {
    method: 'get',
    url: '/_dev/system/list',
    data: {
      sys_code: '${sys_code|default:undefined}',
      sys_name: '${sys_name|default:undefined}',
      sys_status: '${sys_status|default:undefined}',
      page: '${page|default:undefined}',
      perPage: '${perPage|default:undefined}',
      orderBy: '${orderBy|default:undefined}',
      orderDir: '${orderDir|default:undefined}',
    },
  },
  headerToolbar: ['reload', 'bulkActions', 'filter-toggler', addDialog, importDialog],
  itemActions: [],
  footerToolbar: [
    'statistics',
    {
      type: 'pagination',
      layout: 'perPage,pager,go',
    },
  ],
  syncLocation: false,
  filterTogglable: true,
  primaryField: 'id',
  filter: filter,
  columns: [
    {
      name: 'sys_code',
      label: '系统编号',
      width: 80,
      sortable: true,
    },
    {
      name: 'sys_name',
      label: '系统名称',
      width: 150,
      sortable: true,
    },
    {
      type: 'service',
      name: 'module_interface_count',
      label: '模块/接口个数',
      width: 100,
      body: {
        type: 'tpl',
        tpl: '${module_count}/${interface_count}',
      },
    },

    {
      name: 'sys_status',
      label: '状态',
      type: 'mapping',
      map: {
        0: "<span class='label label-default'>停用</span>",
        1: "<span class='label label-success'>启用</span>",
      },
      width: 50,
    },
    {
      type: 'operation',
      label: '操作',
      buttons: [editDialog, delDialog, enableButton, disableButton],
      width: 200,
    },
  ],
}

let systemCrud = {
  type: 'crud',
  mode: 'table',
  perPage: 10,
  autoFillHeight: true,
  affixHeader: true,
  columnsTogglable: true,
  alwaysShowPagination: true,
  api: {
    method: 'get',
    url: '/_dev/system/list',
    data: {
      sys_code: '${sys_code|default:undefined}',
      sys_name: '${sys_name|default:undefined}',
      sys_leader: '${sys_leader|default:undefined}',
      sys_desc: '${sys_desc|default:undefined}',
      sys_status: '${sys_status|default:undefined}',
      page: '${page|default:undefined}',
      perPage: '${perPage|default:undefined}',
      orderBy: '${orderBy|default:undefined}',
      orderDir: '${orderDir|default:undefined}',
    },
  },
  headerToolbar: ['reload', 'bulkActions', 'filter-toggler', addDialog, importDialog],
  itemActions: [],
  footerToolbar: [
    'statistics',
    {
      type: 'pagination',
      layout: 'perPage,pager,go',
    },
  ],
  syncLocation: false,
  filterTogglable: true,
  primaryField: 'id',
  filter: filter,
  columns: [
    {
      name: 'sys_code',
      label: '系统编号',
      width: 150,
      sortable: true,
    },
    {
      name: 'sys_name',
      label: '系统名称',
      width: 200,
      sortable: true,
    },
    {
      name: 'module_count',
      label: '模块数',
    },
    {
      name: 'resource_count',
      label: '资源数',
    },
    {
      name: 'interface_count',
      label: '接口数',
    },
    {
      name: 'sys_leader',
      label: '负责人',
      width: 80,
    },
    {
      name: 'sys_desc',
      label: '系统描述',
    },
    {
      name: 'sys_status',
      label: '状态',
      type: 'mapping',
      map: {
        0: "<span class='label label-default'>停用</span>",
        1: "<span class='label label-success'>启用</span>",
      },
      width: 50,
    },
    {
      type: 'operation',
      label: '操作',
      buttons: [editDialog, delDialog, enableButton, disableButton, exportLink],
      width: 200,
    },
  ],
}


export { systemCrud };
